//
//  infoView.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-15.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "infoView.h"
#import "commondef.h"
#import "UserSingleton.h"

@implementation infoView

@synthesize delegate;

- (void)dealloc
{
    [navBar release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        
        navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, screenrc.size.width, NAV_HEIGHT)];
        
        [self addSubview: navBar];
       
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"我的资料"];
        [navBar pushNavigationItem:navItem animated:YES];
        
        [navItem release];
        
        logoffbtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT + 120.0f, 
                                                              100.0f, 60.0f)];
        [logoffbtn setTitle:@"注销" forState:UIControlStateNormal];
        [logoffbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [logoffbtn addTarget:self action:@selector(logoutclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:logoffbtn];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    
    CGRect screenrc = [[UIScreen mainScreen] bounds];
    if(CGRectContainsPoint(CGRectMake(0.0, 
                                      20.0 + NAV_HEIGHT,
                                      screenrc.size.width, 
                                      screenrc.size.height - 20.0 - NAV_HEIGHT - 80.0),
                           location))
    {
    }
}

#pragma mark- button click
-(void)logoutclick:(id)sender
{ 
    if([[NSFileManager defaultManager] removeItemAtPath:[UserSingleton sharedSingleton].filePath error:NULL])
    {
        [UserSingleton sharedSingleton].bhasUser = NO;
        NSLog(@"%@,%@",[UserSingleton sharedSingleton].curUserName,
              [UserSingleton sharedSingleton].curUserPs);
        
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"恭喜" 
                                                          message:@"注销成功" 
                                                         delegate:self
                                                cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [myAlert show]; 
    }
}

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                         NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

#pragma mark-alertdelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.title == @"恭喜") 
    {
        [delegate  TurnToLogin];
    }
    [alertView release];
}
@end
